﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL.Entities;
using System.Data.SqlClient;

namespace DAL.EntitiesDAL
{
    public class DauSachDAL : BaseDAL, UTL.IBaseDAL
    {
        

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_Dausach");
        }

        public int Count()
        {
            throw new NotImplementedException();
        }

       
        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Dausach {0}";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (DauSach)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Dausach '{0}', N'{1}', N'{2}', N'{3}', N'{4}', '{5}', N'{6}', '{7}', '{8}', '{9}' ";
            sql = string.Format(sql, o.MADAUSACH, o.TENDAUSACH, o.NOIDUNG_TT, o.NGONNGU, o.NAMXUATBAN.ToString("dd/MM/yyyy"), o.LANXUATBAN, o.KICHTHUOC, o.SOTRANG, o.MATHELOAI, o.MANHAXB);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (DauSach)obj;
            var sql = "set dateformat dmy; execute sp_Update_Dausach '{0}', N'{1}', N'{2}', '{3}', N'{4}', '{5}', N'{6}', '{7}', '{8}', '{9}' ";
            sql = string.Format(sql, o.MADAUSACH, o.TENDAUSACH, o.NOIDUNG_TT, o.NGONNGU, o.NAMXUATBAN, o.LANXUATBAN, o.KICHTHUOC, o.SOTRANG, o.MATHELOAI, o.MANHAXB);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }
        
        public DataTable Search(string name)
        {
            var sql = @"Select *  from tb_Dausach a inner join tb_Theloai b on a.MATHELOAI=b.MATHELOAI where a.MATHELOAI like N'%{0}%' ";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search_NXB(string name)
        {
            var sql = @"Select *  from tb_Dausach a inner join tb_Nhaxuatban b on a.MANHAXB=b.MANHAXB where a.MANHAXB like N'%{0}%' ";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }


        public DataTable Search(DAL.Entities.DauSach o)
        {
            string sql;
            if (o.MADAUSACH != "")
                sql = @"Select * from tb_Dausach Where MADAUSACH like '%" + o.MADAUSACH + "%'  ";
            else 
                     sql = @"Select * from tb_Dausach Where TENDAUSACH like N'%" + o.TENDAUSACH + "%' or NOIDUNG_TT like N'" + o.NOIDUNG_TT + "' ";

                     sql += " or NGONNGU like N'" + o.NGONNGU + "' ";
                    
                     sql += " or KICHTHUOC like '" + o.KICHTHUOC + "' or  MATHELOAI like '" + o.MATHELOAI + "' or MANHAXB like '" + o.MANHAXB + "' ";
                 
            return ExecuteQuery(sql);
        }

        #region Other

        public DataTable Select_Max()
        {
            var sql = @"Select Max(MADAUSACH) as Ma From tb_Dausach";
            return ExecuteQuery(sql);
        }


        #endregion


        public DataTable Select1()
        {
            return ExecuteQuery("sp_SelectAll_Thongkesach");
        }

        public DataTable Search_TheoMaDauSach(string name)
        {
            var sql = "set dateformat dmy; execute sp_SelectAll_Thongkesach_TheoMaDauSach " + name + " ";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }
        
    }
}
